
angular.module("CommonServiceMod", [])
    .filter('cutString', function() {
        return function (value, max, tail) {
            var judgeLength = 0,cutString = new String();
            var re = /^[\u4E00-\u9FA5]$/;
            if (!value){
                return ''
            };
            max = parseInt(max);
            for (var i = 0; i < value.length; i++) {
                if (judgeLength >= max) {
                    cutString = cutString.substr(0, cutString.length-1);
                    cutString = cutString.concat(tail||"..");
                    return cutString;
                }

                var singleChar = value.charAt(i);
                if (re.test(singleChar)) {
                    judgeLength+=2;
                    console.log(singleChar);
                }
                else{
                    judgeLength++;
                }
                cutString = cutString.concat(singleChar);
            }
            return cutString;
        };
    })
    .factory('asyncService', ['$q', function($q) {
        return {
            copyToClipboard: function(text) {
                return new Promise((resolve, reject) => {
                    navigator.clipboard.writeText(text)
                        .then(() => {
                            resolve('复制成功');
                        })
                        .catch((err) => {
                            reject('复制失败: ' + err);
                        });
                });
            },
            pasteFromClipboard: function() {
                return new Promise((resolve, reject) => {
                    navigator.clipboard.readText()
                        .then((text) => {
                            resolve(text);
                        })
                        .catch((err) => {
                            reject('无法粘贴文本: ' + err);
                        });
                });
            }
        };
    }])
    .factory('maskService', function () {
        return {
            show: function (content) {
                document.getElementById('maskTitle').innerHTML = content;
                document.getElementById('mask').classList.add('visible');                
            },
            hide: function () {
                document.getElementById('maskTitle').innerHTML = "正在加载中";
                document.getElementById('mask').classList.remove('visible');
            }
        };
    });